<template>
  <div class="disposal-request-page">
    <el-card>
      <div class="header-bar">
        <el-input v-model="searchForm.registerId" placeholder="按挂号ID查询" style="width: 200px; margin-right: 12px;" />
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="resetSearch">重置</el-button>
        <el-button type="success" @click="openAddDialog">新增处置申请</el-button>
      </div>
      <el-table :data="tableData" style="width: 100%; margin-top: 18px;" border>
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="registerId" label="挂号ID" width="100" />
        <el-table-column prop="medicalTechnologyId" label="医技项目ID" width="120" />
        <el-table-column prop="disposalInfo" label="处置信息" />
        <el-table-column prop="disposalPosition" label="处置部位" />
        <el-table-column prop="creationTime" label="创建时间" width="160" />
        <el-table-column prop="disposalEmployeeId" label="处置医生ID" width="120" />
        <el-table-column prop="disposalState" label="状态" width="100" />
        <el-table-column prop="disposalRemark" label="备注" />
        <el-table-column label="操作" width="180">
          <template #default="{ row }">
            <el-button size="small" @click="openEditDialog(row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="500px">
      <el-form :model="editForm" label-width="120px">
        <el-form-item label="挂号ID">
          <el-input v-model="editForm.registerId" />
        </el-form-item>
        <el-form-item label="医技项目ID">
          <el-input v-model="editForm.medicalTechnologyId" />
        </el-form-item>
        <el-form-item label="处置信息">
          <el-input v-model="editForm.disposalInfo" />
        </el-form-item>
        <el-form-item label="处置部位">
          <el-input v-model="editForm.disposalPosition" />
        </el-form-item>
        <el-form-item label="处置医生ID">
          <el-input v-model="editForm.disposalEmployeeId" />
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="editForm.disposalState" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="editForm.disposalRemark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSave">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {
  getAllDisposalRequests,
  searchDisposalRequests,
  addDisposalRequest,
  updateDisposalRequest,
  deleteDisposalRequest,
  getDisposalRequestsByRegisterId
} from '@/api/disposalRequest'
import { ElMessage, ElMessageBox } from 'element-plus'

const tableData = ref([])
const searchForm = ref({ registerId: '' })
const dialogVisible = ref(false)
const dialogTitle = ref('')
const editForm = ref({})

// 查询所有
const fetchAll = async () => {
  const res = await getAllDisposalRequests()
  tableData.value = res.data || []
}

// 按条件查询
const handleSearch = async () => {
  if (searchForm.value.registerId) {
    const res = await getDisposalRequestsByRegisterId(searchForm.value.registerId)
    tableData.value = res.data || []
  } else {
    fetchAll()
  }
}

const resetSearch = () => {
  searchForm.value.registerId = ''
  fetchAll()
}

// 新增弹窗
const openAddDialog = () => {
  dialogTitle.value = '新增处置申请'
  editForm.value = {
    registerId: '',
    medicalTechnologyId: '',
    disposalInfo: '',
    disposalPosition: '',
    disposalEmployeeId: '',
    disposalState: '',
    disposalRemark: ''
  }
  dialogVisible.value = true
}

// 编辑弹窗
const openEditDialog = (row) => {
  dialogTitle.value = '编辑处置申请'
  editForm.value = { ...row }
  dialogVisible.value = true
}

// 保存（新增/编辑）
const handleSave = async () => {
  try {
    if (editForm.value.id) {
      await updateDisposalRequest(editForm.value.id, editForm.value)
      ElMessage.success('编辑成功')
    } else {
      await addDisposalRequest(editForm.value)
      ElMessage.success('新增成功')
    }
    dialogVisible.value = false
    fetchAll()
  } catch (e) {
    ElMessage.error(e.message || '操作失败')
  }
}

// 删除
const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除该处置申请吗？', '提示', {
    type: 'warning'
  }).then(async () => {
    await deleteDisposalRequest(id)
    ElMessage.success('删除成功')
    fetchAll()
  }).catch(() => {})
}

// 初始化
fetchAll()
</script>

<style scoped>
.disposal-request-page {
  padding: 24px;
}
.header-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
</style>
